﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LightWeightORM.ORM.TableAccessor;
using SmartWarehouse.Common;
using SmartWarehouse.ORM.Entity;
using SmartWarehouse.BussinessLogic.Basis;

public partial class Shipment : SwBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            InitMovementType();
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        InventoryFacade invFacade = new InventoryFacade();

        DataTable dtInv = invFacade.GetInventory(this.txtName.Text.Trim());

        this.gvInventory.DataSource = dtInv;
        this.gvInventory.DataBind();
    }
    protected void gvInventory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        ClearMessage();
        this.plShipment.Visible = true;
    }
    protected void InitMovementType()
    {
        MovementTypeFacade mvtFacade = new MovementTypeFacade();

        SwMovementType[] mvts = mvtFacade.GetMovementType(false);

        foreach (SwMovementType mvt in mvts)
        {
            drpMovementType.Items.Add(new ListItem(mvt.Desc, mvt.Name));
        }
    }
    protected void btnShip_Click(object sender, EventArgs e)
    {
        this.ClearMessage();
        SwMessage message = new SwMessage(SwMessageType.Ok, "已成功出库");

        GridViewRow shipRow = gvInventory.SelectedRow;
        string productName = shipRow.Cells[0].Text;
        string curWarehouse = shipRow.Cells[1].Text;
        string curLocation = shipRow.Cells[2].Text;
        string curBin = shipRow.Cells[3].Text;
        decimal invQty = decimal.Parse(shipRow.Cells[4].Text);

        decimal shipQty = decimal.Parse(this.txtShipQty.Text);
        //Input Check
        if (shipQty > invQty)
        {
            message.MessageType = SwMessageType.Error;
            message.MessageText = string.Format("出库数量必须小于库存数量，出库数量：{0}，库存数量：{1}", invQty, shipQty);
            this.ShowMessage(message);
            this.txtShipQty.Focus();
            return;
        }

        SwTransaction trans = new SwTransaction();
        trans.Product_name = productName;
        trans.Cur_warehouse = curWarehouse;
        trans.Cur_location = curLocation;
        trans.Cur_bin = curBin;
        trans.Movement_type = this.drpMovementType.SelectedValue;
        trans.Qty = shipQty;
        trans.User = this.UserName;
        trans.Datetime = DateTime.Now;

        TransactionFacade transFacade = new TransactionFacade();
        message = transFacade.AddTransaction(trans);
        this.ShowMessage(message);
        this.btnQuery_Click(sender, e);

        HideShipmentPanel();
    }

    private void HideShipmentPanel()
    {
        this.txtShipQty.Text = string.Empty;
        this.drpMovementType.SelectedIndex = 0;
        this.plShipment.Visible = false;
    }
}
